package org.wx.zk.unconf;

import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.wx.zk.comm.ZKUtils;

import java.util.concurrent.CountDownLatch;

/**
 * @description: 统一配置
 * @author: wangxun
 * @createDate: 2021/3/23
 * @version: 1.0
 */
@Slf4j
public class TestUnConf {
    ZooKeeper zk;
    UnConfWatcher watcher;
    RedisConf redisConf;
    @Before
    public void init(){
        zk = ZKUtils.getZK();
        redisConf = new RedisConf();
        watcher = new UnConfWatcher();
    }

    @Test
    public void getConf ()throws Exception{

        watcher.setWatchPath("/pay");
        watcher.setRedisConf(redisConf);
        watcher.setZk(zk);
        watcher.setCountDownLatch(new CountDownLatch(1));

        watcher.await();

        while (true){
            // 走到这步，说明门栓打开了，redisConf 应该有值
            final String conf = redisConf.getConf();
            if("".equals(conf) || null == conf){
                log.info("配置信息没有了,重新阻塞，等待配置信息同步");
                watcher.await();
            }else{
                // 有配置信息
                log.info("获取到配置信息：{}",conf);
            }
            // 休息一秒，等待数据的同步
            Thread.sleep(1000);
        }
    }

    @After
    public void close(){
        ZKUtils.closeZK();
    }
}
